// pages/details/details.js
let {
  getGoodDetail
} = require('../../api/detailsApi');

let shopCtrl = require('../../utils/shopCtrl');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodInf: {},
    isok: false,
    range: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    pickValue: 1,
    value: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    // let gid = 552370716;
    let {gid} = options;
    this.getGoodDetail(gid);
  },

  getGoodDetail(gid) {
    getGoodDetail(gid).then(res => {
      console.log(res.data);
      if (res.data.code == 200) {
        //正确获取商品详情信息
        let goodinf = res.data.data;
        let str = goodinf.bodys;
        if (str) {
          //只有str是有内容的，代表这个商品是有详情介绍的
          var srcReg = /src=\"([^\"]*?)\"/gi;
          var arr = str.match(srcReg);
          var imglist = [];
          if (arr) {
            //证明详情里面有图片
            arr.forEach(item => {
              let imginf = item.slice(5, -1);
              imglist.push(imginf);
            });
            goodinf.imglist = imglist;
          }
        }
        console.log('详情数据，全', goodinf);
        this.setData({
          goodInf: goodinf
        });
      }
    })
  },

  goIndex() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  goShop() {
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },

  getNum(ev) {
    console.log(ev);
    let {
      value
    } = ev.detail;
    let pickValue = this.data.range[value];
    console.log("数量是", pickValue);
    this.setData({
      pickValue
    })
  },

  toggleModal() {
    let {
      isok
    } = this.data;
    this.setData({
      isok: !isok
    });
  },

  son() {},

  addCard(ev) {
    // 1.隐藏模板
    let {
      isok
    } = this.data;
    this.setData({
      isok: !isok
    });
    // 2.数据添加到购物车里
    let {gid,gtitle,gprice,gnum,gimgurl} = ev.target.dataset;
    let goodInfo = {
      gid,
      gtitle,
      gprice,
      gnum,
      gimgurl,
      gchecked: true
    }
    shopCtrl.addCardData(goodInfo);
    // // 2.1.
    // let goodsList = shopCtrl.getCartData();
    // // 2.2.
    // // console.log(ev.target.dataset);
    // let {gid,gtitle,gprice,gnum,gimgurl} = ev.target.dataset;
    // let goodInfo = {
    //   gid,
    //   gtitle,
    //   gprice,
    //   gnum,
    //   gimgurl,
    //   gchecked: true
    // }
    // console.log('商品全部数据', goodInfo);
    // // 2.3.添加新数据
    // goodsList.push(goodInfo);
    // // 3.存储数据
    // shopCtrl.setCartData(goodsList);
  }
})